﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FisheryPlatform.Entity;
using FisheryPlatform.DataAccess.Post;
using FisheryPlatform.Bussiness.Option;

namespace FisheryPlatform.Bussiness.Post
{
    public class PostManager
    {
        public static bool Save(PostMixEntity postMixInfo)
        {
            return PostsDB.Save(postMixInfo);
        }

        public static PostMixEntity GetByPostId(long postId)
        {
            return PostsDB.GetByPostId(postId);
        }

        public static List<PostsEntity> Search(string sqlWhere, int pageIndex, int pageSize, out int pageCount)
        {
            return PostsDB.Search(sqlWhere, pageIndex, pageSize, out pageCount);
        }

        public static Dictionary<long, List<TermEntity>> GetPostTermRelationshipsByPostIds(string postIds)
        {
            return PostTermRelationshipsDB.GetListByPostIds(postIds);
        }

        public static List<PostsEntity> GetPublishList(string postIds)
        {
            var sqlWhere = " postId in(" + postIds + ") and status=" + ((int)PostStatusEnum.Publish);
            var pageCount = 0;
            return Search(sqlWhere, 0, 0, out pageCount);
        }

        /// <summary>
        /// 首页推荐
        /// </summary>
        /// <returns></returns>
        public static List<PostsEntity> HomeList()
        {
            var postIds = OptionSingleton.Instance.Get(OptionNameConstants.Posts_Home).Value;
            if (string.IsNullOrEmpty(postIds) == false)
            {
                return GetPublishList(postIds);
            }
            return new List<PostsEntity>();
        }

        /// <summary>
        /// 列表置顶
        /// </summary>
        /// <returns></returns>
        public static List<PostsEntity> TopList()
        {
            var postIds = OptionSingleton.Instance.Get(OptionNameConstants.Posts_Top).Value;
            if (string.IsNullOrEmpty(postIds) == false)
            {
                return GetPublishList(postIds);
            }
            return new List<PostsEntity>();
        }

        /// <summary>
        /// 获取各种状态下的Post数量
        /// </summary>
        /// <returns></returns>
        public static Dictionary<int, int> GetAmountGroupByStatus()
        {
            return PostsDB.GetAmountGroupByStatus();
        }

        public static bool BatchUpdateStatus(string postIds, PostStatusEnum status)
        {
            return PostsDB.BatchUpdateStatus(postIds, status);
        }
    }
}
